package com.niiwoo.civet.account.service.local.check.job.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.File;

@ConfigurationProperties(AccountCheckingProperties.PREFIX)
public class AccountCheckingProperties {
	public static final String PREFIX = "account-checking";
	/** 平台商户编号 **/
	private String plamtformNo;
	/** 对账文件zip所在目录 **/
	private String basePath;
	/** 对账文件后缀 **/
	private String suffix = ".txt";
	/** 对账文件zip包前缀 **/
	private String zipPrefix = "AccountChecking_";
	/** 对账文件zip包后缀 **/
	private String zipSuffix = ".zip";
	/** 对账文件名间隔符 **/
	private String checkFileSeparator = "_";
	/** 是否排除首行数据 **/
	private boolean removeFirst = true;
	/** 每次加载对账数据大小 **/
	private int loadDataLimit = 1000;
	/** 对账线程池队列大小 **/
	private int queueLoadDataLimit = 2000;
	/** 循环加载对账数据线程休眠时间 **/
	private long loadDataSleepTime = 1000;
	/** 对账任务线程池线程数量，默认16 **/
	private int checkThreadNum = 16;
	/** 批量入库记录数，默认100 **/
	private int batch2dbCapacity = 100;
	/** 对账记录ID **/
	private long checkRecordId;
	/** 对账日期 **/
	private String checkDate;
	/** 解析对账文件入库时是否需要删除之前的数据；默认false **/
	private boolean reloadData = false;
	/** 下载对账文件异常时重试次数 **/
	private int downloadRetryNum = 3;
	/** 下载对账文件异常时重试时间间隔，默认30s **/
	private long downloadRetryPriod = 30 * 1000L;

	public String getPlamtformNo() {
		return plamtformNo;
	}

	public void setPlamtformNo(String plamtformNo) {
		this.plamtformNo = plamtformNo;
	}

	private String getBasePath() {
		if (basePath == null || "".equals(basePath)) {
			String path = AccountCheckingProperties.class.getProtectionDomain().getCodeSource().getLocation().getFile();
			int idx;
			if (path.indexOf("target/classes") > 0)
			{
				idx = path.indexOf("target/classes");
				
				path = path.substring(0, idx);
				path += "target/tmp";
			} else if (path.indexOf("/lib/") > 0) {
				idx = path.indexOf("/lib/");
				
				path = path.substring(0, idx);
				path += "/tmp";
			} else if (path.endsWith("/")) {
				idx = path.lastIndexOf("/");
				
				path = path.substring(0, idx);
				path += "/tmp";
			} else {
				idx = path.indexOf("/");
				
				path = path.substring(0, idx);
				path += "/tmp";
			}
			
			basePath = path;
		}
		return basePath;
	}
	
	public String getCheckFilePath(String checkFileType) {
		StringBuffer buf = new StringBuffer();
		buf.append(getBasePath()).append(File.separator);
		buf.append(zipPrefix).append(checkDate).append(File.separator);
		buf.append(checkDate).append(checkFileSeparator);
		buf.append(plamtformNo).append(checkFileSeparator).append(checkFileType).append(suffix);
		
		return buf.toString();
	}
	
	public String getCheckFileDir() {
		StringBuffer buf = new StringBuffer();
		buf.append(getBasePath()).append(File.separator);
		buf.append(zipPrefix).append(checkDate).append(File.separator);
		
		return buf.toString();
	}
	
	public String getCheckZipFile() {
		StringBuffer buf = new StringBuffer();
		buf.append(getBasePath()).append(File.separator);
		buf.append(zipPrefix).append(checkDate).append(zipSuffix);
		
		return buf.toString();
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getCheckFileSeparator() {
		return checkFileSeparator;
	}

	public void setCheckFileSeparator(String checkFileSeparator) {
		this.checkFileSeparator = checkFileSeparator;
	}

	public boolean isRemoveFirst() {
		return removeFirst;
	}

	public void setRemoveFirst(boolean removeFirst) {
		this.removeFirst = removeFirst;
	}

	public int getLoadDataLimit() {
		return loadDataLimit;
	}

	public void setLoadDataLimit(int loadDataLimit) {
		this.loadDataLimit = loadDataLimit;
	}

	public int getQueueLoadDataLimit() {
		return queueLoadDataLimit;
	}

	public void setQueueLoadDataLimit(int queueLoadDataLimit) {
		this.queueLoadDataLimit = queueLoadDataLimit;
	}

	public long getLoadDataSleepTime() {
		return loadDataSleepTime;
	}

	public void setLoadDataSleepTime(long loadDataSleepTime) {
		this.loadDataSleepTime = loadDataSleepTime;
	}

	public String getCheckDate() {
		return checkDate;
	}

	public void setCheckDate(String checkDate) {
		this.checkDate = checkDate;
	}

	public int getCheckThreadNum() {
		return checkThreadNum;
	}

	public void setCheckThreadNum(int checkThreadNum) {
		this.checkThreadNum = checkThreadNum;
	}
	
	public String getZipSuffix() {
		return zipSuffix;
	}

	public void setZipSuffix(String zipSuffix) {
		this.zipSuffix = zipSuffix;
	}

	public int getBatch2dbCapacity() {
		return batch2dbCapacity;
	}

	public void setBatch2dbCapacity(int batch2dbCapacity) {
		this.batch2dbCapacity = batch2dbCapacity;
	}

	public String getZipFilePath() {
		StringBuffer buf = new StringBuffer();

		buf.append(basePath).append(File.separator);
		buf.append(zipPrefix).append(checkDate).append(zipSuffix);;
		
		return buf.toString();
	}

	public long getCheckRecordId() {
		return checkRecordId;
	}

	public void setCheckRecordId(long checkRecordId) {
		this.checkRecordId = checkRecordId;
	}

	public String getZipPrefix() {
		return zipPrefix;
	}

	public void setZipPrefix(String zipPrefix) {
		this.zipPrefix = zipPrefix;
	}

	public boolean isReloadData() {
		return reloadData;
	}

	public void setReloadData(boolean reloadData) {
		this.reloadData = reloadData;
	}

	public int getDownloadRetryNum() {
		return downloadRetryNum;
	}

	public void setDownloadRetryNum(int downloadRetryNum) {
		this.downloadRetryNum = downloadRetryNum;
	}

	public long getDownloadRetryPriod() {
		return downloadRetryPriod;
	}

	public void setDownloadRetryPriod(long downloadRetryPriod) {
		this.downloadRetryPriod = downloadRetryPriod;
	}
	
}
